package juc.FixedThreadPool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 如何合理地估算线程池大小
 * 如果是CPU密集型应用，则线程池大小设置为N+1
 * 如果是IO密集型应用，则线程池大小设置为2N+1
 * http://ifeve.com/how-to-calculate-threadpool-size/
 */
public class Main {

    public static void main(String[] args) {

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

        for (int i = 0; i < 5; i++) {
            final int taskId = i;
            fixedThreadPool.execute(() -> {
                for (int j = 0; j < 5; j++) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("第" + taskId + "次任务的第" + j + "次执行");
                }
            });
        }




    }
}
